BattleCal = {}

require "math"

function BattleCal.cal(atkRole, defRole)
	return atkRole.atk
end

function BattleCal.selectTarget(friendGroup)
	local targetGroup = nil
	if(friendGroup) then
		targetGroup = RoleManager.team
	else
		targetGroup = RoleManager.enemy
	end

	for i=1,#targetGroup do
		if(not targetGroup[i]:isDead()) then
			return targetGroup[i]
		end
	end
end

function BattleCal.randomSeed()
	math.randomSeed(os.time())
end

function BattleCal.calSkillChance(chance)
	local randomResult = math.random(100)
	if(randomResult <= chance) then
		return true
	end

	return false
end

function BattleCal.selectTarget(selectType, isFriend, role, spellTarget) -- 选择目标类型
	local targetTable = {}

	if(selectType == SelectTargetType.RandomSelectEnemy) then
		local targetGroup = nil
		if(isFriend) then
			targetGroup = RoleManager.enemy
		else
			targetGroup = RoleManager.team
		end
		local num = #targetGroup
		if(num > 0) then
			local randomResult = math.random(num)
			table.insert(targetTable, targetGroup[randomResult])
		end

	elseif(selectType == SelectTargetType.LeastHpFriend) then
		local targetGroup = nil
		if(isFriend) then
			targetGroup = RoleManager.team
		else
			targetGroup = RoleManager.enemy
		end
		local leastHpRole = nil
		local leastHp = 1000000  --最大的正数
		for _,v in ipairs(targetGroup) do
			if(leastHp > v.hp) then
				leastHpRole = v
				leastHp = v.hp
			end
		end
		if(leastHpRole ~= nil) then
			table.insert(targetTable, leastHpRole)
		end

	elseif(selectType == SelectTargetType.LeastHpEnemy) then
		local targetGroup = nil
		if(isFriend) then
			targetGroup = RoleManager.enemy
		else
			targetGroup = RoleManager.team
		end
		local leastHpRole = nil
		local leastHp = 1000000  --应为最大的正数
		for _,v in ipairs(targetGroup) do
			if(leastHp > v.hp) then
				leastHpRole = v
				leastHp = v.hp
			end
		end
		if(leastHpRole ~= nil) then
			table.insert(targetTable, leastHpRole)
		end

	elseif(selectType == SelectTargetType.Self) then
		table.insert(targetTablem, role)

	elseif(selectType == SelectTargetType.AllEnemy) then
		local targetGroup = nil
		if(isFriend) then
			targetGroup = RoleManager.enemy
		else
			targetGroup = RoleManager.team
		end
		if(#targetGroup > 0) then
			for i,v in ipairs(targetGroup) do
				table.insert(targetTable, v)
			end
		end

	elseif(selectType == SelectTargetType.AllFriend) then
		local targetGroup = nil
		if(isFriend) then
			targetGroup = RoleManager.team
		else 
			targetGroup = RoleManager.enemy
		end
		if(#targetGroup > 0) then
			for i,v in ipairs(targetGroup) do
				table.insert(targetTable, v)
			end
		end

	elseif(selectType == SelectTargetType.All) then
		if(#RoleManager.team > 0 or #RoleManager.enemy > 0) then
			for i,v in ipairs(RoleManager.team) do
				table.insert(targetTable, v)
			end

			for i,v in ipairs(RoleManager.enemy) do
				table.insert(targetTable, v)
			end
		end
	elseif(selectType == SelectTargetType.SkillTarget) then 
		if(role.curSkillTarget ~= nil) then
			table.insert(targetTable, role.curSkillTarget)
		end
	elseif(selectType == SelectTargetType.SpellTarget) then
		if(spellTarget ~= nil) then
			table.insert(targetTable, spellTarget)
		end
	end

	return targetTable
end

function BattleCal.sortActions(sortTable, team, enemy)
	tableUtil.clearTable(sortTable)

	for _,v in ipairs(team) do
		for i=1,#v.speed do
			local actionElement = {}
			actionElement.speed = v.speed[i]
			actionElement.role = v
			table.insert(sortTable, actionElement)
		end
	end

	for _,v in ipairs(enemy) do
		for i=1,#v.speed do
			local actionElement = {}
			actionElement.speed = v.speed[i]
			actionElement.role = v
			table.insert(sortTable, actionElement)
		end
	end

	local length = #sortTable
	for i=1, length - 1 do
		for j=i + 1,length do
			if(sortTable[i].speed.value < sortTable[j].speed.value) then
				sortTable[i], sortTable[j] = sortTable[j], sortTable[i]
			end
		end
	end
end

function BattleCal.checkSkill(role, speed, roundNum)
	--[[
	local speedTable = role:getSpeed(speed)
	if(speedTable == nil) then
		GameLog.Warning("role " + role.name + " doesn't have speed " + speed)
		return nil
	end
	]]
	local speedTable = speed

	local skillList = speedTable.skills
	for _,v in ipairs(skillList) do
		local skillData = role.skills[v]
		if(skillData == nil) then
			GameLog.Warning("role "..role.name.." doesn't have skill "..v)
			return nil
		end

		local skillConfig = skillData.skill

		-- 回合触发类技能
		if(skillConfig.trigger_type == 2) then
			if(skillData.triggerRound < 0) then
				if(skillConfig.trigger_param <= roundNum) then
					return v
				end
			elseif(roundNum - skillData.triggerRound >= skillConfig.cd) then
				return v
			end
		end

		-- 几率触发类技能
		if(skillConfig.trigger_type == 1) then
			if(skillData.triggerRound < 0 or roundNum - skillData.triggerRound >= skillConfig.cd) then
				if(BattleCal.calSkillChance(skillConfig.trigger_param)) then
					return v
				end
			end
		end
	end

	return nil
end

function BattleCal.calDamage(skillParam, min, max, extra)
	local randomValue = math.random(min, max)
	local damageValue = skillParam * randomValue + extra
	return damageValue
	--return 50
end

function BattleCal.calHeal(min, max, extra)
	local randomValue = math.random(min, max)
	local damageValue = randomValue + extra
	return damageValue
end

function BattleCal.calBlockDamage(damageValue)
	return damageValue * 0.5
end
